package ihooyah.com.whitehat.device;

import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.telephony.data.ApnSetting;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by debby on 2018/12/28.
 * 描述:
 */
public class DeviceMethod {
    private DeviceFeedBackListener feedBackListener;

    private DevicePolicyManager devicePolicyManager;
    private ComponentName componentName;
    private Context mContext;

    DeviceMethod(Context context) {
        mContext = context;
        //获取设备管理服务
        devicePolicyManager = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
        //DeviceReceiver 继承自 DeviceAdminReceiver
        componentName = new ComponentName(context, DeviceReceiver.class);
    }

    public DeviceMethod(Context context, DeviceFeedBackListener feedBackListener) {
        mContext = context;
        this.feedBackListener = feedBackListener;
        //获取设备管理服务
        devicePolicyManager = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
        //DeviceReceiver 继承自 DeviceAdminReceiver
        componentName = new ComponentName(context, DeviceReceiver.class);
    }

    // 激活程序
    void onActivate() {
        Toast.makeText(mContext, "激活", Toast.LENGTH_SHORT).show();
        //判断是否激活  如果没有就启动激活设备
        if (!isAdminActive()) {
            Intent intent = new Intent(
                    DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
                    componentName);
            intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "提示文字");
            mContext.startActivity(intent);
        } else {
            Toast.makeText(mContext, "设备已经激活,请勿重复激活", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 移除程序 如果不移除程序 APP无法被卸载
     */
    public void onRemoveActivate() {
        devicePolicyManager.removeActiveAdmin(componentName);
    }

    /**
     * 设置解锁方式 不需要激活就可以运行
     */
    public void startLockMethod() {
        Intent intent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
        mContext.startActivity(intent);
    }

    /**
     * 设置解锁方式
     */
    public void setLockMethod() {
        // PASSWORD_QUALITY_ALPHABETIC
        // 用户输入的密码必须要有字母（或者其他字符）。
        // PASSWORD_QUALITY_ALPHANUMERIC
        // 用户输入的密码必须要有字母和数字。
        // PASSWORD_QUALITY_NUMERIC
        // 用户输入的密码必须要有数字
        // PASSWORD_QUALITY_SOMETHING
        // 由设计人员决定的。
        // PASSWORD_QUALITY_UNSPECIFIED
        // 对密码没有要求。
        if (isAdminActive()) {
            Intent intent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
            devicePolicyManager.setPasswordQuality(componentName,
                    DevicePolicyManager.PASSWORD_QUALITY_NUMERIC);
            mContext.startActivity(intent);
        }
    }

    /**
     * 立刻锁屏
     */
    public void lockNow() {
        if (isAdminActive()) {
            devicePolicyManager.lockNow();
        }
    }

    /**
     * 设置多长时间后锁屏
     *
     * @param time 时间
     */
    public void lockByTime(long time) {
        if (isAdminActive()) {
            devicePolicyManager.setMaximumTimeToLock(componentName, time);
        }
    }

    /**
     * 恢复出厂设置
     */
    public void wipeData() {
        if (isAdminActive()) {
            try {
                devicePolicyManager.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);
            } catch (SecurityException ex) {
                ex.printStackTrace();
                if (feedBackListener != null) {
                    feedBackListener.onError("当前管理员无法删除数据");
                }
            }
        }
    }

    /**
     * 设置密码锁
     *
     * @param password 密码
     */
    public void setPassword(String password) {
        if (isAdminActive()) {
            try {
                devicePolicyManager.resetPassword(password,
                        DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY);
            } catch (SecurityException ex) {
                ex.printStackTrace();
                if (feedBackListener != null) {
                    feedBackListener.onError("管理员无法更改当前密码");
                }
            }
        }
    }


    /**
     * 禁止使用所有设备摄像头
     *
     * @param disabled 是否禁用摄像头
     */
    public void disableCamera(boolean disabled) {
        if (isAdminActive()) {
            devicePolicyManager.setCameraDisabled(componentName, disabled);
        }
    }

    /**
     * 设备摄像头是否已禁用
     *
     * @return true：已禁用
     */
    public Boolean getCameraDisabled() {
        if (isAdminActive()) {
            return devicePolicyManager.getCameraDisabled(componentName);
        }
        return false;
    }


    public void addCrossProfileIntentFilter() {
        if (isAdminActive()) {
//            devicePolicyManager.clearCrossProfileIntentFilters(componentName);
            try {
                IntentFilter filter = new IntentFilter(Intent.ACTION_SEND);
                filter.addDataType("text/plain");
                filter.addDataType("image/jpeg");
                devicePolicyManager.addCrossProfileIntentFilter(componentName
                        , filter, DevicePolicyManager.FLAG_MANAGED_CAN_ACCESS_PARENT
                                | DevicePolicyManager.FLAG_PARENT_CAN_ACCESS_MANAGED);
            } catch (IntentFilter.MalformedMimeTypeException e) {
                e.printStackTrace();
            }
        }
    }


    public void setProfileName() {
    }


    public boolean isAdminActive() {
        if (devicePolicyManager.isAdminActive(componentName)) {
            return true;
        } else {
            Toast.makeText(mContext, "请先开启毁灭模式", Toast.LENGTH_SHORT).show();
            return false;
        }
    }

}
